package com.example.mydy.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.mydy.R;
import com.example.mydy.model.UserRewardPrivilege;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 * 用户特权奖励适配器
 * 替代旧的UserPrivilegeAdapter，提供更好的特权管理功能
 */
public class UserRewardPrivilegeAdapter extends RecyclerView.Adapter<UserRewardPrivilegeAdapter.ViewHolder> {
    private Context context;
    private List<UserRewardPrivilege> privileges;
    private OnPrivilegeActionListener actionListener;

    public UserRewardPrivilegeAdapter(Context context) {
        this.context = context;
        this.privileges = new ArrayList<>();
    }

    public UserRewardPrivilegeAdapter(Context context, List<UserRewardPrivilege> privileges) {
        this.context = context;
        this.privileges = privileges != null ? privileges : new ArrayList<>();
    }

    public void setOnPrivilegeActionListener(OnPrivilegeActionListener listener) {
        this.actionListener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_user_privilege, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        UserRewardPrivilege privilege = privileges.get(position);
        
        // 设置用户信息
        holder.username.setText(privilege.getUsername());
        holder.remark.setText(privilege.getRemark());
        
        // 设置特权状态
        boolean isValid = privilege.isValid();
        holder.status.setText(isValid ? "有效" : "已过期");
        holder.status.setBackgroundResource(isValid ? 
                R.drawable.bg_status_active : R.drawable.bg_status_expired);
        
        // 设置分成比例
        holder.level1Ratio.setText(String.format(Locale.getDefault(), "%.1f%%", privilege.getLevel1Ratio()));
        holder.level2Ratio.setText(String.format(Locale.getDefault(), "%.1f%%", privilege.getLevel2Ratio()));
        holder.level3Ratio.setText(String.format(Locale.getDefault(), "%.1f%%", privilege.getLevel3Ratio()));
        
        // 设置邀请奖励
        holder.inviteReward.setText(String.format(Locale.getDefault(), "¥%.2f", privilege.getInviteReward()));
        
        // 设置有效期
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
        String validPeriod;
        if (privilege.getEndDate() == null) {
            validPeriod = "永久有效";
        } else {
            validPeriod = dateFormat.format(privilege.getStartDate()) + " 至 " + 
                    dateFormat.format(privilege.getEndDate());
        }
        holder.validPeriod.setText(validPeriod);
        
        // 设置按钮点击事件
        holder.editButton.setOnClickListener(v -> {
            if (actionListener != null) {
                actionListener.onEdit(privilege, position);
            }
        });
        
        holder.deleteButton.setOnClickListener(v -> {
            if (actionListener != null) {
                actionListener.onDelete(privilege.getId(), position);
            }
        });
        
        // 设置整体点击事件
        holder.itemView.setOnClickListener(v -> {
            if (actionListener != null) {
                actionListener.onClick(privilege);
            }
        });
    }

    @Override
    public int getItemCount() {
        return privileges.size();
    }
    
    public void updateData(List<UserRewardPrivilege> newPrivileges) {
        this.privileges = newPrivileges != null ? newPrivileges : new ArrayList<>();
        notifyDataSetChanged();
    }
    
    public void addPrivilege(UserRewardPrivilege privilege) {
        if (privilege != null) {
            this.privileges.add(privilege);
            notifyItemInserted(privileges.size() - 1);
        }
    }
    
    public void updatePrivilege(UserRewardPrivilege privilege, int position) {
        if (privilege != null && position >= 0 && position < privileges.size()) {
            privileges.set(position, privilege);
            notifyItemChanged(position);
        }
    }
    
    public void removePrivilege(int position) {
        if (position >= 0 && position < privileges.size()) {
            privileges.remove(position);
            notifyItemRemoved(position);
        }
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView username;
        TextView status;
        TextView remark;
        TextView level1Ratio;
        TextView level2Ratio;
        TextView level3Ratio;
        TextView inviteReward;
        TextView validPeriod;
        Button editButton;
        Button deleteButton;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            username = itemView.findViewById(R.id.tv_username);
            status = itemView.findViewById(R.id.tv_status);
            remark = itemView.findViewById(R.id.tv_remark);
            level1Ratio = itemView.findViewById(R.id.tv_level1_ratio);
            level2Ratio = itemView.findViewById(R.id.tv_level2_ratio);
            level3Ratio = itemView.findViewById(R.id.tv_level3_ratio);
            inviteReward = itemView.findViewById(R.id.tv_invite_reward);
            validPeriod = itemView.findViewById(R.id.tv_valid_period);
            editButton = itemView.findViewById(R.id.btn_edit);
            deleteButton = itemView.findViewById(R.id.btn_delete);
        }
    }

    public interface OnPrivilegeActionListener {
        void onClick(UserRewardPrivilege privilege);
        void onEdit(UserRewardPrivilege privilege, int position);
        void onDelete(int id, int position);
    }
} 